package com.api.oauthservices.getrequesttoken;

import org.scribe.exceptions.OAuthException;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.api.client.impl.OAuthProvider;
import com.api.test.OAuthServiceConfig;

public class GetRequestToken {

	private static final ApplicationContext API_CONTEXT = new ClassPathXmlApplicationContext("classpath*:/api-context.xml");

	private final OAuthServiceConfig oauthConfig = API_CONTEXT.getBean(OAuthServiceConfig.class);

	@Test
	public void testWithoutParameters() {
		OAuthRequest request = new OAuthRequest(Verb.POST, oauthConfig.getRequestEndpoint());

		Response response = request.send();

		Assert.assertFalse(response.getCode() >= 200 && response.getCode() < 300);
	}

	@Test
	public void testInvalidVerb() {
		OAuthRequest request = new OAuthRequest(Verb.GET, oauthConfig.getRequestEndpoint());

		Response response = request.send();

		Assert.assertFalse(response.getCode() >= 200 && response.getCode() < 300);
	}

	@Test(expectedExceptions = OAuthException.class)
	public void testInvalidConsumer() {

		OAuthServiceConfig config = new OAuthServiceConfig();
		config.setAccessEndpoint(oauthConfig.getAccessEndpoint());
		config.setAuthorizationEndpoint(oauthConfig.getAuthorizationEndpoint());
		config.setRequestEndpoint(oauthConfig.getRequestEndpoint());

		config.setConsumerKey("aslfkasldfkasdasfdasf");
		config.setConsumerSecret("aslfkasldfkasdasfdasf");

		OAuthProvider provider = new OAuthProvider(config);

		provider.getService().getRequestToken();
	}

	@Test
	public void test() {
		OAuthProvider provider = new OAuthProvider(oauthConfig);

		Token response = provider.getService().getRequestToken();

		Assert.assertNotNull(response);
	}

}
